package com.myapp.api.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myapp.api.entity.StudentCourse;
import com.myapp.api.entity.StudentCourseKey;
import com.myapp.api.service.CourseService;
import com.myapp.api.service.StudentCourseService;
import com.myapp.api.service.StudentService;
import com.myapp.api.tools.JsonResult;
import com.myapp.api.tools.ResultStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Api(value = "StudentCourseController",description = "student_course 表的相关 CURD 操作")
@RestController
@RequestMapping(value = "/student/course", produces = "application/json;charset=UTF-8")
public class StudentCourseController {
    @Autowired
    private StudentCourseService service1;
    @Autowired
    private StudentService service2;
    @Autowired
    private CourseService service3;

    @ApiOperation(value="获取学生选课信息列表",notes="获取所有的学生选课信息",produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "一页多少条", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/all")
    public ResponseEntity<?> getStudentCourses(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        String orderBy = "CreateTime DESC";
        PageHelper.startPage(pageNum, pageSize, orderBy);
        List<StudentCourse> student = service1.getStudentCourses();

        HttpStatus status = HttpStatus.OK;
        PageInfo<StudentCourse> records = new PageInfo<>(student);
        return new ResponseEntity<PageInfo<StudentCourse>>(records, status);
    }

    @ApiOperation(value="新增学生选课信息",notes="新增学生选课信息,不允许提交不存在的学号和课程编号",produces = "application/json")
    @PostMapping
    public ResponseEntity<?> addStudentCourse(@Valid @RequestBody StudentCourse record, BindingResult bindingResult) {
        record.setCreatetime(new Date());

        if (bindingResult.hasErrors()) {
            HttpStatus status = HttpStatus.UNPROCESSABLE_ENTITY;
            JsonResult result = new JsonResult(ResultStatus.PARAMS_ERROR, bindingResult.getFieldError().getDefaultMessage());
            return new ResponseEntity<JsonResult>(result, status);
        } else {
            Integer findSid = service2.getStudentByOneSid(record.getSid());
            Integer findCid = service3.getCourseByOneCid(record.getCid());

            if (!findSid.equals(0)) {
                HttpStatus status = HttpStatus.UNPROCESSABLE_ENTITY;
                JsonResult result = new JsonResult(ResultStatus.PARAMS_ERROR, "不存在的学号" + record.getSid() + " !!!");
                return new ResponseEntity<JsonResult>(result, status);
            } else if (findCid.equals(0)) {
                HttpStatus status = HttpStatus.UNPROCESSABLE_ENTITY;
                JsonResult result = new JsonResult(ResultStatus.PARAMS_ERROR, "不存在的课程编号" + record.getCid() + " !!!");
                return new ResponseEntity<JsonResult>(result, status);
            } else{
                int result = service1.addStudentCourse(record);
                if (result > 0) {
                    HttpStatus status = HttpStatus.OK;
                    JsonResult jsonResult = new JsonResult(ResultStatus.SUCCESS);
                    return new ResponseEntity<JsonResult>(jsonResult, status);
                } else {
                    HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
                    JsonResult jsonResult = new JsonResult(ResultStatus.ADD_DATA_ERROR, "Add StudentCourse Data is Error");
                    return new ResponseEntity<JsonResult>(jsonResult, status);
                }
            }
        }
    }

    @ApiOperation(value="删除学生选课信息",notes="删除学生选课信息",produces = "application/json")
    @DeleteMapping
    public ResponseEntity<?> delStudentCourseById(@Valid @RequestBody StudentCourseKey key, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            HttpStatus status = HttpStatus.UNPROCESSABLE_ENTITY;
            JsonResult result = new JsonResult(ResultStatus.PARAMS_ERROR, bindingResult.getFieldError().getDefaultMessage());
            return new ResponseEntity<JsonResult>(result, status);
        } else {
            int result = service1.deleteStudentCourseById(key);
            if (result >= 0) {
                HttpStatus status = HttpStatus.OK;
                JsonResult jsonResult = new JsonResult(ResultStatus.SUCCESS);
                return new ResponseEntity<JsonResult>(jsonResult, status);
            } else {
                HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
                JsonResult jsonResult = new JsonResult(ResultStatus.DELETE_DATA_ERROR, "Delete StudentCourse Data is Error, the id is" + key.getId() + " !!!");
                return new ResponseEntity<JsonResult>(jsonResult, status);
            }
        }
    }

    @ApiOperation(value="修改学生选课信息",notes="删除学生选课信息,不允许提交不存在的学号和课程编号",produces = "application/json")
    @PutMapping
    public ResponseEntity<?> putStudentCourse(@Valid @RequestBody StudentCourse record, BindingResult bindingResult) {
        record.setUpdatetime(new Date());

        if (bindingResult.hasErrors()) {
            HttpStatus status = HttpStatus.UNPROCESSABLE_ENTITY;
            JsonResult result = new JsonResult(ResultStatus.PARAMS_ERROR, bindingResult.getFieldError().getDefaultMessage());
            return new ResponseEntity<JsonResult>(result, status);
        } else {
            Integer findSid = service2.getStudentByOneSid(record.getSid());
            Integer findCid = service3.getCourseByOneCid(record.getCid());

            if (!findSid.equals(1)) {
                HttpStatus status = HttpStatus.UNPROCESSABLE_ENTITY;
                JsonResult result = new JsonResult(ResultStatus.PARAMS_ERROR, "重复的院系编号" + record.getSid() + " !!!");
                return new ResponseEntity<JsonResult>(result, status);
            } else if (findCid.equals(0)) {
                HttpStatus status = HttpStatus.UNPROCESSABLE_ENTITY;
                JsonResult result = new JsonResult(ResultStatus.PARAMS_ERROR, "不存在的课程编号" + record.getCid() + " !!!");
                return new ResponseEntity<JsonResult>(result, status);
            } else {
                int result = service1.putStudentCourse(record);
                if (result > 0) {
                    HttpStatus status = HttpStatus.OK;
                    JsonResult jsonResult = new JsonResult(ResultStatus.SUCCESS);
                    return new ResponseEntity<JsonResult>(jsonResult, status);
                } else {
                    HttpStatus status = HttpStatus.UNPROCESSABLE_ENTITY;
                    JsonResult jsonResult = new JsonResult(ResultStatus.UPDATE_DATA_ERROR, "Update StudentCourse is error");
                    return new ResponseEntity<JsonResult>(jsonResult, status);
                }
            }
        }
    }
}
